//
//  PlayDetailRecommendListVc.swift
//  TimMusic
//
//  Created by nkl on 2024/10/14.
//

import Foundation
import UIKit
import TSVideoKit

class PlayDetailRecommendListVc: BasicViewController {
    lazy var dismissArea: UIControl = {
        let area = UIControl()
        area.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return area
    }()

    lazy var bgView: UIView = {
        let bg = UIView()
        bg.backgroundColor = .hexColor("#232323")
        bg.layer.cornerRadius = 16
        bg.layer.masksToBounds = true
        return bg
    }()

    lazy var topView: PlayDetailRecommendTopView = PlayDetailRecommendTopView()

    lazy var listVc: SongListViewController = {
        let model = SongRecommendListViewModel()
        let vc = SongListViewController(viewModel: model)
        return vc
    }()

    lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.hexColor("#B8B9C1"), for: .normal)
        btn.titleLabel?.font = .systemFont16
        btn.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        topView.modelBtn.addTarget(self, action: #selector(playModeChange), for: .touchUpInside)
    }
    
    @objc func playModeChange(){
        TSVideoOperator.shared.playerViewModel.loopMode = TSVideoOperator.shared.playerViewModel.loopMode.nextMode()
        self.topView.modelBtn.setImage(UIImage.init(named: TSVideoOperator.shared.playerViewModel.loopMode.icon), for: .normal)
    }

    @objc func dismissCurrentVc() {
        dismiss(animated: true)
    }

    override func makeConstraints() {
        super.makeConstraints()
        dismissArea.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.bottom.equalTo(bgView.snp.top)
        }
        bgView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(view).multipliedBy(0.7)
        }
        topView.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.height.equalTo(50)
        }

        listVc.view.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(closeButton.snp.top)
        }

        closeButton.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(40)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(dismissArea)
        view.addSubview(bgView)
        bgView.addSubview(topView)
        bgView.addSubview(closeButton)
        bgView.addSubview(listVc.view)
    }
}
